/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.jpdftools.ui.managers;

import java.io.File;
import java.util.logging.Level;
import javax.swing.*;
import javax.swing.plaf.basic.BasicColorChooserUI;
import project.jpdftools.managers.LocalizationManager;
import project.jpdftools.managers.LoggerManager;
import project.jpdftools.managers.other.FileUtil;
import project.jpdftools.toolkit.Toolkit;
import project.jpdftools.ui.actionlisteners.ApplyThemeAction;

/**
 *
 * @author Mido
 */
public class ThemeManager {

    private JMenu menu; // the menu that will be used to add the components in
    // themes radio buttons
    // must be static to be able to use them while initializing the program
    private static final JRadioButtonMenuItem rbmiThemesDefault = new JRadioButtonMenuItem("Default");
    private static final JRadioButtonMenuItem rbmiThemesAutumn = new JRadioButtonMenuItem("Autumn");
    private static JMenu mThemesBusiness = new JMenu("Business");
    private static final JRadioButtonMenuItem rbmiThemesBusinessBlackSteel = new JRadioButtonMenuItem("Business Black Steel");
    private static final JRadioButtonMenuItem rbmiThemesBusinessBlueSteel = new JRadioButtonMenuItem("Business Blue Steel");
    private static final JRadioButtonMenuItem rbmiThemesBusinessNormal = new JRadioButtonMenuItem("Business Normal");
    private static final JRadioButtonMenuItem rbmiThemesChallengerDeep = new JRadioButtonMenuItem("Challenger Deep");
    private static JMenu mThemesCreme = new JMenu("Creme");
    private static final JRadioButtonMenuItem rbmiThemesCremeCoffee = new JRadioButtonMenuItem("Creme Coffee");
    private static final JRadioButtonMenuItem rbmiThemesCremeNormal = new JRadioButtonMenuItem("Creme Normal");
    private static JMenu mThemesDust = new JMenu("Dust");
    private static final JRadioButtonMenuItem rbmiThemesDustCoffee = new JRadioButtonMenuItem("Dust Coffee");
    private static final JRadioButtonMenuItem rbmiThemesDustNormal = new JRadioButtonMenuItem("Dust Normal");
    private static final JRadioButtonMenuItem rbmiThemesEmeraldDusk = new JRadioButtonMenuItem("Emerald Dusk");
    private static JMenu mThemesMist = new JMenu("Mist");
    private static final JRadioButtonMenuItem rbmiThemesMistAqua = new JRadioButtonMenuItem("Mist Aqua");
    private static final JRadioButtonMenuItem rbmiThemesMistSilver = new JRadioButtonMenuItem("Mist Silver");
    private static final JRadioButtonMenuItem rbmiThemesModerate = new JRadioButtonMenuItem("Moderate");
    private static JMenu mThemesNebula = new JMenu("Nebula");
    private static final JRadioButtonMenuItem rbmiThemesNebulaBrickWall = new JRadioButtonMenuItem("Nebula Brick Wall");
    private static final JRadioButtonMenuItem rbmiThemesNebulaNormal = new JRadioButtonMenuItem("Nebula Normal");
    private static JMenu mThemesOffice2007 = new JMenu("Office 2007");
    private static final JRadioButtonMenuItem rbmiThemesOffice2007Blue = new JRadioButtonMenuItem("Office 2007 Blue");
    private static final JRadioButtonMenuItem rbmiThemesOffice2007Silver = new JRadioButtonMenuItem("Office 2007 Silver");
    private static final JRadioButtonMenuItem rbmiThemesRaven = new JRadioButtonMenuItem("Raven");
    private static final JRadioButtonMenuItem rbmiThemesSahara = new JRadioButtonMenuItem("Sahara");
    private static final JRadioButtonMenuItem rbmiThemesTwilight = new JRadioButtonMenuItem("Twilight");
    private static final ButtonGroup buttonGroup = new ButtonGroup();

    // constructor with parameters
    public ThemeManager(JMenu menu) {
        this.menu = menu;
        // intilaize the button group component
        buttonGroup.add(rbmiThemesDefault);
        buttonGroup.add(rbmiThemesAutumn);
        buttonGroup.add(rbmiThemesBusinessBlackSteel);
        buttonGroup.add(rbmiThemesBusinessBlueSteel);
        buttonGroup.add(rbmiThemesBusinessNormal);
        buttonGroup.add(rbmiThemesChallengerDeep);
        buttonGroup.add(rbmiThemesCremeCoffee);
        buttonGroup.add(rbmiThemesCremeNormal);
        buttonGroup.add(rbmiThemesDustCoffee);
        buttonGroup.add(rbmiThemesDustNormal);
        buttonGroup.add(rbmiThemesEmeraldDusk);
        buttonGroup.add(rbmiThemesMistAqua);
        buttonGroup.add(rbmiThemesMistSilver);
        buttonGroup.add(rbmiThemesModerate);
        buttonGroup.add(rbmiThemesNebulaBrickWall);
        buttonGroup.add(rbmiThemesNebulaNormal);
        buttonGroup.add(rbmiThemesOffice2007Blue);
        buttonGroup.add(rbmiThemesOffice2007Silver);
        buttonGroup.add(rbmiThemesRaven);
        buttonGroup.add(rbmiThemesSahara);
        buttonGroup.add(rbmiThemesTwilight);
    }

    /**
     * Applies the theme components in the UI
     */
    public void applyThemeInUI() {
        // add menu items to the menu
        menu.add(rbmiThemesDefault);
        menu.add(new JSeparator());
        menu.add(rbmiThemesAutumn);
        mThemesBusiness.add(rbmiThemesBusinessBlackSteel);
        mThemesBusiness.add(rbmiThemesBusinessBlueSteel);
        mThemesBusiness.add(rbmiThemesBusinessNormal);
        menu.add(mThemesBusiness);
        menu.add(rbmiThemesChallengerDeep);
        mThemesCreme.add(rbmiThemesCremeCoffee);
        mThemesCreme.add(rbmiThemesCremeNormal);
        menu.add(mThemesCreme);
        mThemesDust.add(rbmiThemesDustCoffee);
        mThemesDust.add(rbmiThemesDustNormal);
        menu.add(mThemesDust);
        menu.add(rbmiThemesEmeraldDusk);
        mThemesMist.add(rbmiThemesMistAqua);
        mThemesMist.add(rbmiThemesMistSilver);
        menu.add(mThemesMist);
        menu.add(rbmiThemesModerate);
        mThemesNebula.add(rbmiThemesNebulaBrickWall);
        mThemesNebula.add(rbmiThemesNebulaNormal);
        menu.add(mThemesNebula);
        mThemesOffice2007.add(rbmiThemesOffice2007Blue);
        mThemesOffice2007.add(rbmiThemesOffice2007Silver);
        menu.add(mThemesOffice2007);
        menu.add(rbmiThemesRaven);
        menu.add(rbmiThemesSahara);
        menu.add(rbmiThemesTwilight);

        // add action listeners
        rbmiThemesDefault.addActionListener(new ApplyThemeAction("javax.swing.plaf.metal.MetalLookAndFeel"));
        rbmiThemesAutumn.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceAutumnLookAndFeel"));
        rbmiThemesBusinessBlackSteel.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel"));
        rbmiThemesBusinessBlueSteel.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceBusinessBlueSteelLookAndFeel"));
        rbmiThemesBusinessNormal.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel"));
        rbmiThemesChallengerDeep.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceChallengerDeepLookAndFeel"));
        rbmiThemesCremeCoffee.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceCremeCoffeeLookAndFeel"));
        rbmiThemesCremeNormal.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel"));
        rbmiThemesDustCoffee.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceDustCoffeeLookAndFeel"));
        rbmiThemesDustNormal.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceDustLookAndFeel"));
        rbmiThemesEmeraldDusk.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceEmeraldDuskLookAndFeel"));
        rbmiThemesMistAqua.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceMistAquaLookAndFeel"));
        rbmiThemesMistSilver.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceMistSilverLookAndFeel"));
        rbmiThemesModerate.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceModerateLookAndFeel"));
        rbmiThemesNebulaBrickWall.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceNebulaBrickWallLookAndFeel"));
        rbmiThemesNebulaNormal.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceNebulaLookAndFeel"));
        rbmiThemesOffice2007Blue.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel"));
        rbmiThemesOffice2007Silver.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel"));
        rbmiThemesRaven.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceRavenLookAndFeel"));
        rbmiThemesSahara.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceSaharaLookAndFeel"));
        rbmiThemesTwilight.addActionListener(new ApplyThemeAction("org.pushingpixels.substance.api.skin.SubstanceTwilightLookAndFeel"));
    }

    /**
     * initializes previously saved theme of the program
     */
    public static void intializeTheme() {
        File theme_file = new File("Data/Theme.txt");
        // check if the file already exists
        try {
            if (theme_file.exists() == true) {
                // read theme from file
                String theme = new FileUtil().readFromFile("Data/Theme.txt");
                // make sure that the file is not empty
                if (theme != null) {
                    // set the program theme to the one that have been read
                    UIManager.setLookAndFeel(theme);
                    // select/deselect theme radio buttons
                    selectThemeInIntializing(theme);
                    LoggerManager.getLogger().log(Level.INFO, "Selected Theme \"{0}\"", theme);
                } else {
                    // intialize the default theme
                    UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel");
                    // write the default theme in the theme file
                    new FileUtil().writeToFile("Data/Theme.txt", "org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel", false, false);
                    // tell the user that the file is empty
                    JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_THEME_EMPTY"),
                            LocalizationManager.getCurrent_resource().getString("EMPTY_TITLE"), 0);
                    // select/deselect theme radio buttons
                    selectThemeInIntializing("org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel");
                    LoggerManager.getLogger().log(Level.WARNING, "Theme file is empty. Default Theme selected \"{0}\"", "org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel");
                }
            } else {
                // intialize the default theme
                UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel");
                // write the default theme in the theme file
                new FileUtil().writeToFile("Data/Theme.txt", "org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel", false, false);
                // tell the user that the file is missing
                JOptionPane.showMessageDialog(null, LocalizationManager.getCurrent_resource().getString("MAIN_MENU_THEME_MISSING"),
                        LocalizationManager.getCurrent_resource().getString("MISSING_TITLE"), 0);
                // select/deselect theme radio buttons
                selectThemeInIntializing("org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel");
                LoggerManager.getLogger().log(Level.WARNING, "Theme file not found. Default Theme selected \"{0}\"", "org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel");
            }
        } catch (Exception ex) {
            LoggerManager.getLogger().log(Level.SEVERE, "An excption occurred. {0}", Toolkit.getStackTraceAsString(ex.getCause()));
        }
        // TODO: remove this line when the problem with colorChooser LAF is resolved.
        UIManager.put("ColorChooserUI", BasicColorChooserUI.class.getName());
    }

    /**
     * Selects the radio button mapping to the saved theme (used while
     * initializing the program)
     *
     * @param theme
     */
    private static void selectThemeInIntializing(String theme) {
        if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceCremeLookAndFeel") == true) {
            rbmiThemesCremeNormal.setSelected(true);
        } else if (theme.equals("javax.swing.plaf.metal.MetalLookAndFeel") == true) {
            rbmiThemesDefault.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceAutumnLookAndFeel") == true) {
            rbmiThemesAutumn.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceBusinessLookAndFeel") == true) {
            rbmiThemesBusinessNormal.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel") == true) {
            rbmiThemesBusinessBlackSteel.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceBusinessBlueSteelLookAndFeel") == true) {
            rbmiThemesBusinessBlueSteel.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceChallengerDeepLookAndFeel") == true) {
            rbmiThemesChallengerDeep.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceCremeCoffeeLookAndFeel") == true) {
            rbmiThemesCremeCoffee.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceDustLookAndFeel") == true) {
            rbmiThemesDustNormal.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceDustCoffeeLookAndFeel") == true) {
            rbmiThemesDustCoffee.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceEmeraldDuskLookAndFeel") == true) {
            rbmiThemesEmeraldDusk.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceMistAquaLookAndFeel") == true) {
            rbmiThemesMistAqua.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceMistSilverLookAndFeel") == true) {
            rbmiThemesMistSilver.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceModerateLookAndFeel") == true) {
            rbmiThemesModerate.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceNebulaLookAndFeel") == true) {
            rbmiThemesNebulaNormal.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceNebulaBrickWallLookAndFeel") == true) {
            rbmiThemesNebulaBrickWall.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel") == true) {
            rbmiThemesOffice2007Blue.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceOfficeSilver2007LookAndFeel") == true) {
            rbmiThemesOffice2007Silver.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceRavenLookAndFeel") == true) {
            rbmiThemesRaven.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceSaharaLookAndFeel") == true) {
            rbmiThemesSahara.setSelected(true);
        } else if (theme.equals("org.pushingpixels.substance.api.skin.SubstanceTwilightLookAndFeel") == true) {
            rbmiThemesTwilight.setSelected(true);
        }
    }
}
